home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / platformutils.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  6.1 KB  |  184 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import ctypes
  5. import _winreg
  6. import config
  7. import prefs
  8. import os
  9. import logging
  10. import resources
  11. import sys
  12. import urllib
  13. from util import returnsUnicode, returnsBinary, checkU, checkB, call_command, AutoflushingStream
  14. localeInitialized = False
  15. FilenameType = unicode
  16.  
  17. def samefile(path1, path2):
  18.     return getLongPathName(path1) == getLongPathName(path2)
  19.  
  20.  
  21. def getLongPathName(path):
  22.     buf = ctypes.create_unicode_buffer(260)
  23.     GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
  24.     rv = GetLongPathName(path, buf, 260)
  25.     if rv == 0 or rv > 260:
  26.         return path
  27.     else:
  28.         return buf.value
  29.  
  30.  
  31. def getAvailableBytesForMovies():
  32.     moviesDir = config.get(prefs.MOVIES_DIRECTORY)
  33.     freeSpace = ctypes.c_ulonglong(0)
  34.     availableSpace = ctypes.c_ulonglong(0)
  35.     totalSpace = ctypes.c_ulonglong(0)
  36.     rv = ctypes.windll.kernel32.GetDiskFreeSpaceExW(unicode(moviesDir), ctypes.byref(availableSpace), ctypes.byref(totalSpace), ctypes.byref(freeSpace))
  37.     if rv == 0:
  38.         print 'GetDiskFreeSpaceExW failed, returning bogus value!'
  39.         return 0x1900000000L
  40.     
  41.     return availableSpace.value
  42.  
  43. _langs = {
  44.     1025: 'ar',
  45.     1046: 'pt_BR',
  46.     2052: 'zh_CN',
  47.     1028: 'zh_TW',
  48.     1029: 'cs',
  49.     1030: 'da',
  50.     1043: 'nl',
  51.     1035: 'fi',
  52.     1036: 'fr',
  53.     1031: 'de',
  54.     1032: 'el',
  55.     1037: 'he',
  56.     1038: 'hu',
  57.     1040: 'it',
  58.     1041: 'jp',
  59.     1042: 'ko',
  60.     1044: 'nb',
  61.     1045: 'pl',
  62.     2070: 'pt',
  63.     1049: 'ru',
  64.     3082: 'es',
  65.     1053: 'sv',
  66.     1055: 'tr' }
  67.  
  68. def _getLocale():
  69.     code = ctypes.windll.kernel32.GetUserDefaultUILanguage()
  70.     
  71.     try:
  72.         return _langs[code]
  73.     except:
  74.         return None
  75.  
  76.  
  77.  
  78. def initializeLocale():
  79.     global localeInitialized
  80.     lang = _getLocale()
  81.     if lang:
  82.         os.environ['LANGUAGE'] = lang
  83.     
  84.     localeInitialized = True
  85.  
  86. _loggingSetup = False
  87.  
  88. def setupLogging(inDownloader = False):
  89.     global _loggingSetup
  90.     if _loggingSetup:
  91.         return None
  92.     
  93.     if not inDownloader:
  94.         logFile = config.get(prefs.LOG_PATHNAME)
  95.         logStream = open(logFile, 'wt')
  96.         sys.stdout = sys.stderr = AutoflushingStream(logStream)
  97.     else:
  98.         logStream = sys.stderr
  99.     logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s %(levelname)-8s %(message)s', stream = logStream)
  100.     _loggingSetup = True
  101.  
  102.  
  103. def unicodeToFilename(filename, path = None):
  104.     
  105.     def shortenFilename(filename):
  106.         checkU(filename)
  107.         pieces = filename.split(u'.')
  108.         lastpart = pieces[-1]
  109.         if len(pieces) > 1:
  110.             firstpart = u'.'.join(pieces[:-1])
  111.         else:
  112.             firstpart = u''
  113.         if len(firstpart) > 0:
  114.             return u'%s.%s' % (firstpart[:-1], lastpart)
  115.         else:
  116.             return filename[:-1]
  117.  
  118.     shortenFilename = returnsUnicode(shortenFilename)
  119.     checkU(filename)
  120.     if path:
  121.         checkU(path)
  122.     else:
  123.         path = os.getcwd()
  124.     MAX_LEN = 200
  125.     filename.replace('/', '_').replace('\x00', '_').replace('\\', '_').replace(':', '_').replace('*', '_').replace('?', '_').replace('"', '_').replace('<', '_').replace('>', '_').replace('|', '_')
  126.     newFilename = filename
  127.     while len(newFilename) > MAX_LEN:
  128.         newFilename = shortenFilename(newFilename)
  129.     return newFilename
  130.  
  131. unicodeToFilename = returnsUnicode(unicodeToFilename)
  132.  
  133. def filenameToUnicode(filename, path = None):
  134.     if path:
  135.         checkU(path)
  136.     
  137.     checkU(filename)
  138.     return filename
  139.  
  140. filenameToUnicode = returnsUnicode(filenameToUnicode)
  141.  
  142. def osFilenameToFilenameType(filename):
  143.     return FilenameType(filename)
  144.  
  145.  
  146. def osFilenamesToFilenameTypes(filenames):
  147.     return [ osFilenameToFilenameType(filename) for filename in filenames ]
  148.  
  149.  
  150. def filenameTypeToOSFilename(filename):
  151.     return filename
  152.  
  153.  
  154. def makeURLSafe(string, safe = '/'):
  155.     checkU(string)
  156.     return urllib.quote(string.encode('utf_8'), safe = safe).decode('ascii')
  157.  
  158. makeURLSafe = returnsUnicode(makeURLSafe)
  159.  
  160. def unmakeURLSafe(string):
  161.     checkU(string)
  162.     return urllib.unquote(string.encode('ascii')).decode('utf_8')
  163.  
  164. unmakeURLSafe = returnsUnicode(unmakeURLSafe)
  165.  
  166. def resizeImage(source_path, dest_path, width, height):
  167.     """Resize an image to a smaller size.
  168.     
  169.     Guidelines:
  170.  
  171.     Don't try to expand up the image.
  172.  
  173.     Don't change the aspect ratio
  174.  
  175.     The final image should be have the exact dimensions <width>X<height>.  If
  176.     there is extra room, either because the source image was smaller
  177.     specified, or because it had a different aspect ratio, pad out the image
  178.     with black pixels.
  179.     """
  180.     convert_path = os.path.join(resources.appRoot(), '..', 'imagemagick', 'convert.exe')
  181.     border_width = max(width, height) / 2
  182.     call_command(convert_path, source_path, '-strip', '-resize', '%dx%d>' % (width, height), '-gravity', 'center', '-bordercolor', 'black', '-border', '%s' % border_width, '-crop', '%dx%d+0+0' % (width, height), '+repage', dest_path)
  183.  
  184.